package com.laoxu.test.rbacdemo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.laoxu.test.rbacdemo.common.PageRequest;
import com.laoxu.test.rbacdemo.common.Result;
import com.laoxu.test.rbacdemo.constant.AppConstant;
import com.laoxu.test.rbacdemo.exception.AppException;
import com.laoxu.test.rbacdemo.model.SysDict;
import com.laoxu.test.rbacdemo.service.SysDictService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author laoxu
 * @since 2020-08-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/dict")
public class SysDictController extends BaseController{

    private String message;

    @Autowired
    private SysDictService dictService;

    @GetMapping("/list")
    @RequiresPermissions("dict:view")
    public Map<String, Object> DictList(PageRequest request, SysDict dict) {
        return getDataTable(this.dictService.findDicts(request, dict));
    }

    @GetMapping("/trim")
    public Map<String, List<Map<String,Object>>> DictTrimList(SysDict dict) {
        PageRequest request= new PageRequest();
        request.setPageNum(1);
        request.setPageSize(-1);
        IPage<SysDict> dicts = this.dictService.findDicts(request, dict);
        Map<String,List<Map<String,Object>>> _map= new ConcurrentHashMap();
        dicts.getRecords().parallelStream().forEach(_dict->{
            HashMap<String,Object> map=new HashMap<>();
            map.put("keyy",_dict.getKeyy());
            map.put("valuee",_dict.getValuee());
            String key = _dict.getTableName()+ AppConstant.UNDER_LINE+_dict.getFieldName();
            if(!_map.containsKey(key)){
                _map.put(key,new ArrayList<>());
            }
            _map.get(key).add(map);
        });
        return _map;
    }

    @PostMapping("/add")
    @RequiresPermissions("dict:add")
    public Result addDict(@RequestBody @Valid SysDict dict) throws AppException {
        try {
            this.dictService.createDict(dict);
            return Result.ok("新增字典成功");
        } catch (Exception e) {
            message = "新增字典失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

    @PostMapping("/remove")
    @RequiresPermissions("dict:delete")
    public Result deleteDicts(@NotBlank(message = "{required}") @RequestParam String dictIds) throws AppException {
        try {
            String[] ids = dictIds.split(StringPool.COMMA);
            this.dictService.deleteDicts(ids);
            return Result.ok("删除字典成功");
        } catch (Exception e) {
            message = "删除字典失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

    @PostMapping("/modify")
    @RequiresPermissions("dict:update")
    public Result updateDict(@RequestBody @Valid SysDict dict) throws AppException {
        try {
            this.dictService.updateDict(dict);
            return Result.ok("修改字典成功");
        } catch (Exception e) {
            message = "修改字典失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

}
